﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using System.Data.SqlClient;

namespace QLnhasach
{
    public partial class formChinh : XtraForm
    {
        private KetNoiCSDL ketnoi = new KetNoiCSDL();
        public string str_MAKH = "";
        public string str_MALSach = "";

        //private SqlDataReader reader;
        public formChinh(NguoiDung nguoidung)
        {
            InitializeComponent();

            labelThongTin.Text = " Xin Chào: " + nguoidung.tenDangNhap;
            InitSkinGallery();
            showgridviewKH();
            //         DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle("Blue");
        }
        public void showgridviewKH()
        {
            string sql = "select * from KHACHHANG ";
            DataTable dt_KH = ketnoi.LayCSDL(sql);
            grid_viewKH.DataSource = dt_KH;
        }
        public void showgridviewSach()
        {
            string sql = "select * from SACH ";
            DataTable dt_Sach = ketnoi.LayCSDL(sql);
            grid_viewsach.DataSource = dt_Sach;
        }
        public void showgridviewLoaiSach()
        {
            string sql = "select * from LOAISACH ";
            DataTable dt_lSach = ketnoi.LayCSDL(sql);
            grid_viewlsach.DataSource = dt_lSach;
        }
        public void clearKH()
        {
            text_DChi.Text = "";
            text_Makh.Text = "";
            text_SDT.Text = "";
            text_TenKH.Text = "";
            date_NGDKy.Text = "";
            date_NGSinh.Text = "";
         }

        void InitSkinGallery()
        {
            SkinHelper.InitSkinGallery(gl_Giaodien, true);
        }

        private void formChinh_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void btn_Taikhoan_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Taikhoan.PageVisible == false)
            {
                this.gtab_Taikhoan.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Taikhoan;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Taikhoan;
        }

        private void grt_Hienthi_CloseButtonClick(object sender, EventArgs e)
        {
            DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs arg = e as DevExpress.XtraTab.ViewInfo.ClosePageButtonEventArgs;
            (arg.Page as DevExpress.XtraTab.XtraTabPage).PageVisible = false;
        }

        private void barButtonItem15_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Sach.PageVisible == false)
            {
                this.gtab_Sach.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Sach;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Sach;
            //hien danh sach
            showgridviewSach();
        }

        private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Khachhang.PageVisible == false)
            {
                this.gtab_Khachhang.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Khachhang;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Khachhang;
        }


        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Hoadon.PageVisible == false)
            {
                this.gtab_Hoadon.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Hoadon;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Hoadon;
        }

        private void barButtonItem8_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Phieunhap.PageVisible == false)
            {
                this.gtab_Phieunhap.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Phieunhap;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Phieunhap;
        }

        private void barButtonItem9_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Doanhthu.PageVisible == false)
            {
                this.gtab_Doanhthu.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
        }

        private void barButtonItem11_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Doanhthu.PageVisible == false)
            {
                this.gtab_Doanhthu.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
        }

        private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Doanhthu.PageVisible == false)
            {
                this.gtab_Doanhthu.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
        }

        private void barButtonItem12_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Doanhthu.PageVisible == false)
            {
                this.gtab_Doanhthu.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
        }
        private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (this.gtab_Doanhthu.PageVisible == false)
            {
                this.gtab_Doanhthu.PageVisible = true;
                this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
            }
            else this.grt_Hienthi.SelectedTabPage = this.gtab_Doanhthu;
        }
        private void barButtonItem17_ItemClick(object sender, ItemClickEventArgs e)
        {
            showgridviewLoaiSach();
        }

        private void formChinh_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'qLNHASACHDataSet.KHACHHANG' table. You can move, or remove it, as needed.

        }
       // --------------------- Phần xử lý Khách Hàng--------------------------------------

        private void btn_themKH_Click(object sender, EventArgs e)
        {
            string gt = "";
            if (radioButton_Nam.Checked == true)
            {
                gt = "Nam";
            }
            else if (radioButton_Nu.Checked == true)
            {
                gt = "Nu";
            }
            String sql = "insert into KHACHHANG (MAKH,TENKH,DIACHI,GIOITINH,SODT,NGSINH,NGDANGKY) Values ('" + text_Makh.Text + "','" + text_TenKH.Text + "','" + text_DChi.Text + "','" + gt + "','" + text_SDT.Text + "','" + date_NGSinh.Text
              + "','" + date_NGDKy.Text + "')";

            try
            {
                ketnoi.thucthiCSDL(sql);

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            showgridviewKH();
            clearKH();
        }

        private void btn_XóaKH_Click(object sender, EventArgs e)
        {
            string sql_xoa = "delete from KHACHHANG where MAKH = '"+str_MAKH+"'";
            try
            {
                ketnoi.thucthiCSDL(sql_xoa);

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            showgridviewKH();
            clearKH();
        }

     
        private void gridView4_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            
            if (e.RowHandle >= 0)
            {
                str_MAKH=gridView4.GetRowCellValue(e.RowHandle, "MAKH").ToString();
            }
            //*xu lý đưa giá trị kh vào textbox
            text_Makh.Text = gridView4.GetRowCellValue(e.RowHandle, "MAKH").ToString();
            text_TenKH.Text = gridView4.GetRowCellValue(e.RowHandle, "TENKH").ToString();
            text_SDT.Text = gridView4.GetRowCellValue(e.RowHandle, "SODT").ToString();
            text_DChi.Text = gridView4.GetRowCellValue(e.RowHandle, "DIACHI").ToString();
            date_NGDKy.Text = gridView4.GetRowCellValue(e.RowHandle, "NGDANGKY").ToString();
            date_NGSinh.Text = gridView4.GetRowCellValue(e.RowHandle, "NGSINH").ToString();
        }
  

        private void btn_suaKH_Click(object sender, EventArgs e)
        {
           string sql_xoa = "delete from KHACHHANG where MAKH = '"+str_MAKH+"'";
           string gt = "";
           if (radioButton_Nam.Checked == true)
           {
               gt = "Nam";
           }
           else if (radioButton_Nu.Checked == true)
           {
               gt = "Nu";
           }
           String sql = "insert into KHACHHANG (MAKH,TENKH,DIACHI,GIOITINH,SODT,NGSINH,NGDANGKY) Values ('" + text_Makh.Text + "','" + text_TenKH.Text + "','" + text_DChi.Text + "','" + gt + "','" + text_SDT.Text + "','" + date_NGSinh.Text
             + "','" + date_NGDKy.Text + "')";
           try
           {
               ketnoi.thucthiCSDL(sql_xoa);
               ketnoi.thucthiCSDL(sql);
           }
           catch (SqlException ex)
           {
               MessageBox.Show(ex.Message);
           }
           
            
            showgridviewKH();
            clearKH();
        }
        //------------------------Phần xử lý loại sách--------------------------------------------

        private void btn_them_lSach_Click(object sender, EventArgs e)
        {
            String sql = "insert into LOAISACH(MALOAISACH,TENLOAISACH) Values ('" + text_Malsach.Text + "','" + text_tenlsach.Text + "')";
            MessageBox.Show(sql);
            try
            {
                ketnoi.thucthiCSDL(sql);

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            showgridviewLoaiSach();
            text_Malsach.Text = "";
            text_tenlsach.Text = "";
 
        }

        private void btn_xoa_lsach_Click(object sender, EventArgs e)
        {
            string sql_xoalsach = "delete from KHACHHANG where MAKH = '" + str_MALSach + "'";
            try
            {
                ketnoi.thucthiCSDL(sql_xoalsach);

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            showgridviewLoaiSach();
        }

        private void gridView6_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                str_MALSach = gridView6.GetRowCellValue(e.RowHandle, "MALOAISACH").ToString();
            }
        }

       
        
        
     

      

 

      

       
    }
}